//手机号码正则表达式
var phoneReg=/^1[345789][0-9]{9}$/;

//验证码正则表达式
var codeReg=/^[0-9]{6}$/;
var pwdReg0=/^.{6,16}$/;
var pwdReg1=/[0-9]+/;
var pwdReg2=/[a-zA-Z]+/;
var pwdReg3=/[=~!@#\$%\^&\*\(\)_\+{\[\}\]:"\|',<?.\/\\]+/;
var timer;
var t=60;

//表示是否同意注册协议
var isAgree=false;
var isPhoneRight=false;
var isYZMRight=false;
var isPwd1Right=false;
var isPwd2Right=false;

//功能1：向用户提示手机号码格式是否正确
phone.onblur=function(){
    var phoneValue=phone.value;
    if(!phoneReg.test(phoneValue)){
        phoneerror.innerHTML="手机号码格式错误！";
        phoneerror.style.color="#F57A89";
        phoneerror.style.fontSize="12px";
        isPhoneRight=false;
    }else{
        phoneerror.innerHTML="";
        isPhoneRight=true;
    }
    checkAll();
}

//功能2：根据手机号码格式的正确性。决定是否启用验证码按钮
phone.onkeyup=function(){
    var phoneValue=phone.value;
    if(!phoneReg.test(phoneValue)){
        yanzhengmaBtn.setAttribute("disabled","disabled");
        yanzhengmaBtn.style.color="";
        isPhoneRight=false;
    }else{
        yanzhengmaBtn.removeAttribute("disabled");
        yanzhengmaBtn.style.color="#f57a89";
        isPhoneRight=true;
    }
    checkAll();
}

//功能3：发送验证码功能
//点击后要求倒计时60秒，在此期间该按钮应该禁用
yanzhengmaBtn.onclick=function(){
    yanzhengmaBtn.setAttribute("disabled","disabled");
    yanzhengmaBtn.style.color="";
    yanzhengmaBtn.value="重新发送60";

    timer=setInterval(daojishi,1000);
};

function daojishi(){
    t--;
    if(t==0){
        clearInterval(timer);
        yanzhengmaBtn.value="发送验证码";
        yanzhengmaBtn.removeAttribute("disabled");
        yanzhengmaBtn.style.color="#f57a89";
        t=60;
        return;
    }
    yanzhengmaBtn.value="正在获取"+t;
}

//功能4：验证验证码正确性
yzm.onkeyup=function(){
    var yzmValue=yzm.value;
    if(yzmValue.length==0){
        yzmerror.innerHTML="请您填写验证码！";
        yzmerror.style.color="#F57A89";
        yzmerror.style.fontSize="12px";
        isYZMRight=false;
        checkAll();
        return;
    }
    if(!codeReg.test(yzmValue)){
        yzmerror.innerHTML="验证码格式有误！";
        yzmerror.style.color="#F57A89";
        yzmerror.style.fontSize="12px";
        isYZMRight=false;
        checkAll();
        return;
    }
    $.ajax({
        url:"http://test.hfjava.com/shop/yzm",
        dataType:"jsonp",
        success:function(data){
            if(data.code!=yzmValue){
                yzmerror.innerHTML="验证码错误！";
                yzmerror.style.color="#F57A89";
                yzmerror.style.fontSize="12px";
                isYZMRight=false;
            }else{
                yzmerror.innerHTML="验证码正确！";
                yzmerror.style.color="#ffffff";
                yzmerror.style.fontSize="12px";
                isYZMRight=true;
            }
            checkAll();
        }
    });
}

//功能5：密码1验证格式有效性
pwd1.onkeyup=function(){
    var pwd1Value = pwd1.value;
    var rightCount = 0;
    //长度 字母 数字 符号
    var lengthValid=pwdReg0.test(pwd1Value);
    var numberValid=pwdReg1.test(pwd1Value);
    var letterValid=pwdReg2.test(pwd1Value);
    var symbolValid=pwdReg3.test(pwd1Value);
    if(numberValid){
        rightCount++;
    }
    if(letterValid){
        rightCount++;
    }
    if(symbolValid){
        rightCount++;
    }
    if(!lengthValid||rightCount<2){
        pwderror1.innerHTML="密码格式错误";
        pwderror1.style.color="#F57A89";
        pwderror1.style.fontSize="14px";
        isPwd1Right=false;
    }else{
        pwderror1.innerHTML="密码格式正确";
        pwderror1.style.color="#ccc";
        pwderror1.style.fontSize="14px";
        isPwd1Right=true;
    }
    checkPwd2();
    checkAll();
}

//功能6：密码2验证与密码1是否一致
pwd2.onkeyup=function(){
    checkPwd2();
}
function checkPwd2(){
    var pwd1Value=pwd1.value;
    var pwd2Value=pwd2.value;
    if(pwd1Value!==pwd2Value){
        pwderror2.innerHTML="两次输入密码不一致";
        pwderror2.style.color="#F57A89";
        pwderror2.style.fontSize="14px";
        isPwd2Right=false;
    }else{
        pwderror2.innerHTML="两次输入密码相同";
        pwderror2.style.fontSize="14px";
        pwderror2.style.color="#ccc";
        isPwd2Right=true;
    }
    checkAll();
}

//功能7：获得用户是否同意注册协议
agreeCK.onclick=function(){
    isAgree=this.checked;
    checkAll();
}

//功能8：检测是否可以注册
function checkAll(){
    var isRight=isAgree&&isPhoneRight&&isYZMRight&&isPwd1Right&&isPwd2Right;
    if(isRight){
        submitBtn.removeAttribute("disabled");
        submitBtn.style.color="white";
    }else{
        submitBtn.setAttribute("disabled","disabled");
        submitBtn.style.color="";
    }
}

//功能9：实现注册功能
submitBtn.onclick=function(){
    $.post("http://test.hfjava.com/shop/adduser",    $("form").serialize(),
        function(data){
          if(data=="Saved"){
              console.log("注册成功！");
          }
        }
        )
}
